#ifndef BUBBLE_SORT
#define BUBBLE_SORT

#include <algorithm>
#include <iostream>
#include <random>

// 稳定的排序算法

void bubbleSort(int arr[], const int n)
{
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                std::swap(arr[j], arr[j + 1]);
            }
        }
    }
}

int main(int argc, char** argv)
{
    std::random_device rd;
    std::default_random_engine engine(rd());
    std::uniform_int_distribution<int> dis(0, 100);

    const auto arr = new int[100];

    for (int i = 0; i < 100; ++i) {
        arr[i] = dis(engine);
    }

    std::for_each(arr, arr + 100, [](const int a) { std::cout << a << ' '; });

    std::cout << std::endl;

    bubbleSort(arr, 100);

    std::for_each(arr, arr + 100, [](const int a) { std::cout << a << ' '; });

    // 释放动态分配的内存
    delete[] arr;

    return 0;
}

#endif // BUBBLE_SORT
